home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 60.zip / BS1 part 60 / Highspeed pascal.adf / HSPascal / AmigaDemos / Init.pas < prev    next >
Pascal/Delphi Source File  |  1991-12-31  |  12KB  |  377 lines

  1. {--------------------------------------------------------------------------
  2.  
  3.                      HighSpeed Pascal for the Amiga
  4.  
  5.                    ROUTINES FOR DEMO INITIALIZATION
  6.  
  7.                   Programmed by Martin Eskildsen 1991
  8.  
  9.                   Copyright (c) 1991 by D-House I ApS
  10.                          All rights reserved
  11.  
  12.  
  13.   Version : Date (dd.mm.yy) : Comment
  14.   -----------------------------------
  15.     1.00 : 06.11.91 : First version
  16.     1.01 : 16.12.91 : System unit names updated
  17. --------------------------------------------------------------------------}
  18.  
  19. unit Init;
  20.  
  21. INTERFACE
  22.  
  23. uses Exec, Intuition, Graphics, Amiga;
  24.  
  25. const
  26.   ScrWidth  = 640;                  { Size of custom screen }
  27.   ScrHeight = 200;                  { Should not be changed! }
  28.  
  29. function PrepareEnvironment(s : string) : boolean;
  30.                                     { Prepare an environment for the demo }
  31. procedure CloseDown;                { De-init environment }
  32.  
  33. procedure Inform(s : string);       { Write message }
  34. procedure Message(s : string);      { Write message and wait for acknowledement }
  35. procedure WaitMessageClose;         { Wait for msg. window close gadget }
  36.  
  37. function Panic(condition : boolean; s : string) : boolean;
  38.                                     { Write panic message s if condition is }
  39.                                     { TRUE and return condition as result }
  40.  
  41. procedure WaitClose(var w : pWindow);   { Wait for user to click window w's close gadget }
  42. procedure EnableClose(var w : pWindow); { Enable Close messages }
  43. procedure DisableClose(var w : pWindow);{ Disable Close messages }
  44.  
  45. procedure OpenOutputWindow;         { Create a standard demo window }
  46. procedure CloseOutputWindow;        { Remove it again }
  47. procedure ClearOutputWindow;        { Clear work area }
  48.  
  49. function CStrConstPtr(s : string) : pointer;
  50. { This makes s a "C" string, allocates a chunck of heap large enough for s
  51.   to reside therein, puts s in the new memory and returns a pointer to it.
  52.   Please note that the memory is never released again in the program's
  53.   lifespan. This avoids global variables containing screen/window titles. }
  54.  
  55. function RetrieveStr(p : pointer) : string;
  56. { Pick a "C" string from memory pointed at by p and make it Pascal string }
  57.  
  58. function Max(a, b : integer) : integer;
  59. function Min(a, b : integer) : integer;
  60. procedure SwapMin(var a, b : integer);    { Make sure a <= b }
  61.  
  62. function LegalPosition(x, y : integer) : boolean; { Is (x,y) inside output? }
  63.  
  64. function Binary(s : string) : integer;  { Make binary value from s }
  65. { s can contain any character but only 0 and 1 are used for evauation }
  66.  
  67.  
  68. PROCEDURE W(CH:CHAR);
  69.  
  70.  
  71. var
  72.   BaseScreen   : pScreen;
  73.   TopOffset    : integer;           { First raster line usable by demo }
  74.   OutputWinDef : tNewWindow;        { Definition of output window }
  75.                                     { The variable is set up in the }
  76.                                     { unit but made global so the user can }
  77.                                     { alter it before calling }
  78.                                     { OpenOutputWindow. }
  79.   OutputWindow : pWindow;           { The actual output window }
  80.   OutputTitle  : String;            { Output window's title }
  81.   WorkArea     : record             { Actual usable area in window }
  82.                    minX, maxX,
  83.                    minY, maxY  : integer
  84.                  end;
  85.  
  86. IMPLEMENTATION
  87.  
  88.  
  89. PROCEDURE W(CH:CHAR); BEGIN WRITE(CH) END;
  90.  
  91.  
  92. const
  93.   IRev         = 0;                 { Required Intuition revision }
  94.   GRev         = 0;                 { Required Graphics revision }
  95.   Detail       = 0;
  96.   Block        = 1;
  97.  
  98. var
  99.   MsgWindow    : pWindow;           { The message window }
  100.   FontInfo     : tTextAttr;
  101.  
  102. procedure CloseEnvironment;
  103. begin
  104.   CloseWindow(MsgWindow);                 { Remove the message window }
  105.   CloseScreen(BaseScreen);                { and the screen }
  106.   CloseLibrary(pLibrary(IntuitionBase));  { Close Intuition }
  107.   CloseLibrary(pLibrary(GfxBase))         { and Graphics }
  108. end;
  109.  
  110. function PrepareEnvironment(s : string) : boolean;
  111. label 1;                         { Disaster termination point }
  112. var
  113.   status        : boolean;       { TRUE = everything went ok }
  114.   BaseScreenDef : tNewScreen;    { Record defining the custom screen }
  115.   MsgWindowDef  : tNewWindow;    { Record defining the message window }
  116.  
  117.   procedure DefineStdOutputWindow;
  118.   begin
  119.     with OutputWinDef do begin
  120.       LeftEdge    := 10;
  121.       TopEdge     := TopOffset;
  122.       Width       := ScrWidth - 2 * LeftEdge;
  123.       Height      := ScrHeight - TopOffset - 5;
  124.       DetailPen   := Detail;
  125.       BlockPen    := Block;
  126.       Title       := @OutputTitle[1];
  127.       Flags       := WINDOWCLOSE or SMART_REFRESH or WINDOWDEPTH or NOCAREREFRESH;
  128.       IDCMPflags  := CLOSEWINDOW_;
  129.       Type_       := CUSTOMSCREEN;
  130.       FirstGadget := NIL;
  131.       CheckMark   := NIL;
  132.       Screen      := BaseScreen;
  133.       BitMap      := NIL;
  134.       MinWidth    := Width;
  135.       MinHeight   := Height;
  136.       MaxWidth    := MinWidth;
  137.       MaxHeight   := MaxHeight
  138.     end;
  139.     OutputTitle   := 'Output'#0;
  140.     OutputWindow  := NIL
  141.   end;
  142.  
  143. begin
  144.   status := FALSE;
  145.   TopOffset := 0;
  146.  
  147.   IntuitionBase := pIntuitionBase(OpenLibrary('intuition.library', IRev));
  148.                                  { Note the typecast pIntuitionBase(... }
  149.   if Panic(IntuitionBase = NIL, 'intuition.library could not be opened') then goto 1;
  150.  
  151.   GfxBase := pGfxBase(OpenLibrary('graphics.library', GRev));
  152.   if Panic(GfxBase = NIL, 'graphics.library could not be opened') then goto 1;
  153.  
  154.   with FontInfo do begin
  155.     ta_Name    := CStrConstPtr('topaz.font');
  156.     ta_YSize   := TOPAZ_EIGHTY;
  157.     ta_Style   := FS_NORMAL;
  158.     ta_Flags   := FPF_ROMFONT
  159.   end;
  160.  
  161.   with BaseScreenDef do begin
  162.     LeftEdge   := 0;                { MUST be 0! }
  163.     TopEdge    := 0;
  164.     Width      := ScrWidth;
  165.     Height     := ScrHeight;
  166.     Depth      := 2;                { Two bit planes = four colors }
  167.     DetailPen  := Detail;           { Color for details }
  168.     BlockPen   := Block;            { and for blocks }
  169.     ViewModes  := HIRES;            { High resolution }
  170.     Type_      := CUSTOMSCREEN;     { Note the underscore '_' }
  171.     Font       := @FontInfo;        { Use the normal Topaz font }
  172.     DefaultTitle := CStrConstPtr('HighSpeed Pascal for the Amiga! Copyright (c) 1991 by D-House I ApS');
  173.     Gadgets      := NIL;            { No gadgets }
  174.     CustomBitMap := NIL             { No bitmap }
  175.   end;
  176.   BaseScreen := OpenScreen(@BaseScreenDef);  { Note the @ operator }
  177.   if Panic(BaseScreen = NIL, 'Could not open demo screen') then begin
  178.     CloseLibrary(pLibrary(IntuitionBase));   { No screen! Close libs and }
  179.     CloseLibrary(pLibrary(GfxBase));         { get out of here! }
  180.     goto 1
  181.   end;
  182.  
  183.   with MsgWindowDef do begin
  184.     LeftEdge      := 10;
  185.     TopEdge       := 15;
  186.     Width         := ScrWidth - 2 * LeftEdge;
  187.     Height        := 28;
  188.     DetailPen     := Detail;
  189.     BlockPen      := Block;
  190.     Title         := CStrConstPtr('Messages. Use the Close gadget to accept/continue');
  191.     Flags         := WINDOWCLOSE or       { Add Close gadget and }
  192.                      WINDOWDEPTH or       { depth arrangement gadgets }
  193.                      SMART_REFRESH or     { Save window in RAM }
  194.                      ACTIVATE or          { Activate it }
  195.                      NOCAREREFRESH;       { Don't wanna hear of refreshes! }
  196.     IDCMPFlags    := CLOSEWINDOW_;        { But of user-clicks on Close }
  197.     Type_         := CUSTOMSCREEN;        { Put window in custom screen }
  198.     FirstGadget   := NIL;                 { No gadgets attached }
  199.     CheckMark     := NIL;                 { Same checkmark as usual }
  200.     Screen        := BaseScreen;          { Use our own custom screen }
  201.     BitMap        := NIL;                 { No bitmap }
  202.     MinWidth      := Width;               { Dummies as we can't resize }
  203.     MinHeight     := Height;              { this window }
  204.     MaxWidth      := MinWidth;
  205.     MaxHeight     := MinHeight
  206.   end;
  207.   MsgWindow := OpenWindow(@MsgWindowDef);
  208.   if Panic(MsgWindow = NIL, 'Can not open message window') then begin
  209.     CloseEnvironment;
  210.     goto 1
  211.   end;
  212.   DisableClose(MsgWindow);                { See WINDOW1.PAS for explanation }
  213.  
  214.   status := TRUE;       { No Gurus! (yet...) Announce it to the world }
  215.  
  216.   TopOffset := MsgWindowDef.TopEdge +  { Top of window plus }
  217.                MsgWindowDef.Height  +  { window's height plus }
  218.                10;                     { a margin }
  219.  
  220.   DefineStdOutputWindow;
  221.   Message('Welcome to the ' + s + ' Demo!');
  222.  
  223.   1:                    { Where to go if the world turns against you }
  224.   PrepareEnvironment := status
  225. end;
  226.  
  227. procedure CloseDown;
  228. begin
  229.   if OutputWindow <> NIL then             { <> NIL = window still on screen }
  230.     CloseOutputWindow;                    { so we close it }
  231.   Message('That''s all folks!');
  232.   CloseEnvironment
  233. end;
  234.  
  235. procedure Inform(s : string);
  236. begin
  237.   while length(s) < 73 do s := s + ' ';   { Pad with spaces (simple, eh?) }
  238.   s := copy(s, 1, 73);                    { Truncate string to 73 chars }
  239.   with MsgWindow^ do begin
  240.     Move_(RPort, 20, 20);                 { Put text at (20,20) }
  241.     Text_(RPort, s, length(s))            { Output it }
  242.   end
  243. end;
  244.  
  245. procedure WaitClose(var w : pWindow);              { Wait for the user to }
  246. var dummy : integer;                               { click the Close gadget }
  247. begin                                              { in window w }
  248.   EnableClose(w);
  249.   dummy := Wait(BitMask(w^.UserPort^.MP_SIGBIT));
  250.   DisableClose(w)
  251. end;
  252.  
  253. procedure EnableClose(var w : pWindow);
  254. begin
  255.   with w^ do IDCMPflags := IDCMPflags or CLOSEWINDOW_
  256. end;
  257.  
  258. procedure DisableClose(var w : pWindow);
  259. begin
  260.   with w^ do IDCMPflags := IDCMPflags and not CLOSEWINDOW_
  261. end;
  262.  
  263. procedure Message(s : string);
  264. begin
  265.   Inform(s);                              { Output message }
  266.   WaitClose(MsgWindow)                    { Wait for Close }
  267. end;
  268.  
  269. procedure WaitMessageClose;
  270. begin
  271.   WaitClose(MsgWindow)
  272. end;
  273.  
  274. function Panic(condition : boolean; s : string) : boolean;
  275. begin
  276.   if condition then Message('Demo problem: ' + s + ' - terminating soon.');
  277.   Panic := condition
  278. end;
  279.  
  280. procedure OpenOutputWindow;
  281. begin
  282.   OutputTitle := OutputTitle + #0;        { Just to be sure }
  283.   OutputWinDef.Title := @OutputTitle[1];
  284.   OutputWindow := OpenWindow(@OutputWinDef);
  285.   if Panic(OutputWindow = NIL, 'Can''t open output window') then begin
  286.     CloseDown;
  287.     halt(0)
  288.   end;
  289.   SetApen(OutputWindow^.RPort, 3);
  290.   with OutputWindow^, WorkArea do begin
  291.     minX := BorderLeft;
  292.     minY := BorderTop;
  293.     maxX := Width  - BorderRight;
  294.     maxY := Height - BorderBottom
  295.   end
  296. end;
  297.  
  298. procedure CloseOutputWindow;
  299. begin
  300.   CloseWindow(OutputWindow);
  301.   OutputWindow := NIL
  302. end;
  303.  
  304. procedure ClearOutputWindow;
  305. begin
  306.   with WorkArea, OutputWindow^ do begin
  307.     SetAPen(RPort, 0);
  308.     RectFill(RPort, minX, minY, maxX, maxY);
  309.     SetAPen(RPort, 3)
  310.   end
  311. end;
  312.  
  313. function CStrConstPtr(s : string) : pointer;
  314. type a = packed array [0..255] of char;
  315. var  p : ^a;
  316. begin
  317.   s := s + #0;                         { Make "C" string }
  318.   getmem(p, length(s));                { Get some mem for it }
  319.   move(s[1], p^, length(s));           { Move s into newly alloc'd mem }
  320.   CStrConstPtr := p                    { Return the pointer }
  321. end;
  322.  
  323. function RetrieveStr(p : pointer) : string;
  324. type
  325.   a = packed array [0..255] of char;
  326. var
  327.   i    : integer;
  328.   sptr : ^a;
  329.   s    : string;
  330. begin
  331.   sptr := p;
  332.   s := '';
  333.   i := 0;
  334.   while sptr^[i] <> #0 do begin
  335.     s := s + sptr^[i];
  336.     inc(i)
  337.   end;
  338.   RetrieveStr := s
  339. end;
  340.  
  341. function Max(a, b : integer) : integer;
  342. begin
  343.   if a > b then Max := a else Max := b
  344. end;
  345.  
  346. function Min(a, b : integer) : integer;
  347. begin
  348.   if a < b then Min := a else Min := b
  349. end;
  350.  
  351. procedure SwapMin(var a, b : integer);
  352. var t : integer;
  353. begin
  354.   if a > b then begin
  355.     t := a;
  356.     a := b;
  357.     b := t
  358.   end
  359. end;
  360.  
  361. function LegalPosition(x, y : integer) : boolean;
  362. begin
  363.   with WorkArea do LegalPosition := (x >= minX) and (x <= maxX) and
  364.                                     (y >= minY) and (y <= maxY)
  365. end;
  366.  
  367. function Binary(s : string) : integer;
  368. var i, n : integer;
  369. begin
  370.   n := 0;
  371.   for i := 1 to length(s) do
  372.     if s[i] in ['0', '1'] then n := n*2 + ord(s[i]) - ord('0');
  373.   Binary := n
  374. end;
  375.  
  376. end.
  377.